<template>
  <div class="gwwaichu">
    <div class="toolbar">
      <router-link to="/main/process" style="">返回</router-link>
      <span style="display: grid;grid-template-columns: repeat(3, 1fr);gap: 10px;">
        <span v-if="!isView" @click="saveData">保存</span>
        <span @click="printForm">打印</span>
        <router-link to="/main/renshi/kaoqing/gwwaichu-list" style="">列表</router-link>
      </span>
    </div>
    <h2>公务外出申请单</h2>
    <table border="1">
      <tbody>
        <!-- 基本信息 -->
        <tr>
          <td>姓 名</td>
          <td><input v-model="formData.name" :disabled="isView" /></td>
          <td>职 务</td>
          <td><input v-model="formData.position" :disabled="isView" /></td>
          <td>部 门</td>
          <td><input v-model="formData.department" :disabled="isView" /></td>
        </tr>
        <!-- 外出事由和地点 -->
        <tr>
          <td>外出事由</td>
          <td colspan="3"><textarea v-model="formData.reason" :disabled="isView"></textarea></td>
          <td>外出地点</td>
          <td><input v-model="formData.location" :disabled="isView" /></td>
        </tr>
        <!-- 审核和记录 -->
        <tr>
          <td>部门经理审核</td>
          <td><input v-model="formData.managerApproval" :disabled="isView" /></td>
          <td>企业管理部记录</td>
          <td colspan="3"><input v-model="formData.managementRecord" :disabled="isView" /></td>
        </tr>
      </tbody>
    </table>
    <p style="display: flex;line-height: 30px;">注：</p>
    <ol>
      <li>1.外出时，《公务外出单》须交企业管理部人资组备案；</li>
      <li>2.业务员、部门经理不受此限；</li>
      <li>3.若部门经理外出，由企业管理部部长批准。</li>
    </ol>
    <p class="footer" style="line-height: 30px;display: flex;justify-content: space-between;"><span>表单编号及版本：AM-JY-183
        A/0</span><span>
        保存期限：长期</span></p>
  </div>
</template>

<script>
import { saveBusinessTrip, updateBusinessTrip, getBusinessTripDetail } from '@/api/businessTrip';

export default {
  data() {
    return {
      formData: {
        id: null,
        name: '',
        position: '',
        department: '',
        reason: '',
        location: '',
        managerApproval: '',
        managementRecord: ''
      },
      isView: false,
      isEdit: false
    };
  },
  created() {
    const id = this.$route.query.id;
    const type = this.$route.query.type;

    if (id) {
      this.isView = type === 'view';
      this.isEdit = type === 'edit';
      this.fetchData(id);
    }
  },
  methods: {
    printForm() {
      window.print();
    },
    async fetchData(id) {
      try {
        const response = await getBusinessTripDetail(id);
        if (response.data) {
          this.formData = { ...response.data };
        }
      } catch (error) {
        this.$message.error('获取数据失败：' + error.message);
      }
    },
    async saveData() {
      try {
        // 验证必填字段
        if (!this.formData.name) {
          throw new Error('请填写姓名');
        }
        if (!this.formData.position) {
          throw new Error('请填写职务');
        }
        if (!this.formData.department) {
          throw new Error('请填写部门');
        }
        if (!this.formData.reason) {
          throw new Error('请填写外出事由');
        }
        if (!this.formData.location) {
          throw new Error('请填写外出地点');
        }

        if (this.isEdit) {
          // 编辑模式
          await updateBusinessTrip(this.formData);
          this.$message.success('更新成功');
        } else {
          // 新增模式
          await saveBusinessTrip(this.formData);
          this.$message.success('保存成功');
        }
        this.$router.push('/main/renshi/kaoqing/gwwaichu-list');
      } catch (error) {
        this.$message.error('保存失败：' + error.message);
      }
    }
  }
};
</script>

<style scoped lang="scss">
.gwwaichu {
  width: 1200px;
  margin: 0 auto;
  max-width: 1200px;
  height: 100vh;
}

.toolbar {
  display: flex;
  justify-content: space-between;
  padding-top: 10px;
}

table {
  border-collapse: collapse;
  width: 100%;
  text-align: center;
}

th,
td {
  border: 1px solid #ddd;
  padding: 8px;
}

textarea {
  width: 100%;
}

input[disabled],
textarea[disabled] {
  background-color: #f5f7fa;
  border-color: #e4e7ed;
  color: #909399;
  cursor: not-allowed;
}

ol li {
  display: flex;
  line-height: 30px;
}

@media print {

  /*@page规则被用来设置打印页面的方向为横向*/
  // @page {
  //   size: landscape;
  // }
  h2 {
    text-align: center;
    width: 88%;
  }

  .container {
    padding: 20px 10px 20px 1px;
    margin-right: 5px;
    box-shadow: 0 0px 0px rgba(255, 255, 255, 0) !important;
  }

  .toolbar {
    display: none;
  }

  .page-header {
    .header-actions {
      display: none;
    }
  }

  .main-form {
    box-shadow: none;
    padding: 10px;
  }

  table {
    margin: 10px 0px;
    width: 88%;
  }

  .info-table,
  .content-table,
  .approval-table {
    border: 1px solid #000;

    td,
    th {
      border: 1px solid #000;
    }
  }

  td,
  th {
    padding: 5px;
    border: 1px solid #000;
  }

  input {
    border: none !important;
    background-color: transparent !important;
    text-align: center;
  }

  textarea {
    border: 0px !important;
    resize: none;
  }

  .footer {
    width: 88%;
  }
}
</style>